export function getChunk(data,size){
    /*
        列表分组
     */
    var len = parseInt(size);
    let groups = []
    if (len <= 1 || data.length < len){
        groups = [data]
    }
    else{
        let loop = Math.ceil(data.length / len);
        for (var i = 0; i < loop; i++){
            groups.push(data.slice(len * i, len * (i + 1)));
        }

    }
    return groups;
};

export function merge(left,right){
    /*
        归并算法
     */

    let res = []
    let max_length = Math.max(left.length,right.length)
    for( let i=0; i < max_length; i++){
        if(i < left.length ){
            res.push(left[i])
        }
        if(i < right.length ){
            res.push(right[i])
        }
    }

    return res;
};

export function getMixedList(products,random_ad_list,product_chunk_size){
    /*
     */
    if(typeof product_chunk_size != "number" || parseInt(product_chunk_size) < 2){
        product_chunk_size = 4
    }
    else{
        product_chunk_size = parseInt(product_chunk_size)
    }
    let groups = getChunk(products,product_chunk_size)
    let mixedList = merge(groups.map( item =>{
        return {
            "className":"product-list",
            "type":"product-block",
            "products":item
        }
    }),random_ad_list.map( item =>{
        let doc = {
            "wxBannerAd":"wx-ad banner-ad",
            "wxGridAd":"wx-ad grid-ad",
            "wxVideoAd":"wx-ad video-ad",
        }
        item.className = doc[item.type] || "";
        return item
    }))

    return mixedList
}